package com.pj.hellomvvm.ui.fragment.other

import android.os.Bundle
import android.view.View
import com.blankj.utilcode.util.ToastUtils
import com.pj.common.annotation.LayoutParam
import com.pj.common.base.BaseViewModel
import com.pj.common.base.topbar.BaseTopBarFragment
import com.pj.hellomvvm.R
import com.pj.hellomvvm.databinding.FragmentOtherMmkvBinding
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog.Builder.ICON_TYPE_FAIL
import com.tencent.mmkv.MMKV

@LayoutParam(layout = R.layout.fragment_other_mmkv, title = R.string.title_fragment_mmkv)
class OtherMMKVFragment : BaseTopBarFragment<BaseViewModel, FragmentOtherMmkvBinding>() {

    override fun initData(view: View, savedInstanceState: Bundle?) {
        super.initData(view, savedInstanceState)
        val mmkv = MMKV.defaultMMKV()
        mCBinding.apply {
            btnSave.setOnClickListener {
                val text = etInput.text.toString()
                if (text.isNullOrBlank()) {
                    showTip("请输入数据")
                } else {
                    mmkv.encode("TEXT", text)
                    showTip("保存成功")
                }
            }
            btnShow.setOnClickListener {
                val text = mmkv.decodeString("TEXT", "没有值，显示默认")
                etInput.setText(text)
            }
            btnClear.setOnClickListener {
//                mmkv.clearAll()
            }
        }
    }

    fun showTip(msg: String) {
        val tip = QMUITipDialog.Builder(requireContext()).setTipWord(msg).create(true)
        tip.show()
        mCBinding.root.postDelayed({ tip.dismiss() }, 1500)
    }
}